package co.edu.usbcali.raju.games.chess.model;

import java.util.Hashtable;

import org.apache.log4j.Logger;

import jpl.Compound;
import jpl.Query;
import jpl.Term;
import jpl.Variable;
import co.edu.usbcali.raju.games.chess.helpers.PlBoardWrapper;
import co.edu.usbcali.raju.model.Board;
import co.edu.usbcali.raju.model.IEvaluator;

public class EvaluateThreat implements IEvaluator {
	
	private static EvaluateThreat instance;
	private static Logger logger=Logger.getLogger(EvaluateThreat.class.getName());;


	public static EvaluateThreat getInstance() {
		if(instance==null){
			instance= new EvaluateThreat();
		}
		return instance;
	}

	@Override
	public double evaluate(Board board,String player) {
		ChessBoard cBoard=(ChessBoard)board;
//		double val=PlBoardWrapper.getInstance().evaluateThreat(cBoard);
		double val=evaluateThreat(cBoard);
		return val;
	}
	
	public double evaluateThreat(ChessBoard board){
		Term boardS=(Term)PlBoardWrapper.getInstance().exportBoard(board);
		Term statusS=(Term)PlBoardWrapper.getInstance().exportStatus(board);
		Variable evaluation=new Variable(PlBoardWrapper.VALUE);
//		Compound evaluate=new Compound(EVALUATE_THREAD_BOARD, new Term[]{boardS,statusS,new Atom(((ChessBoard)board).getPreviousTurn()),evaluation});
		Compound evaluate=new Compound(PlBoardWrapper.EVALUATE_THREAD_BOARD, new Term[]{boardS,statusS,evaluation});
		logger.debug("Consultando:"+Compound.toString(new Term[]{evaluate}));
		Query query=new Query(evaluate);
		query.open();
		Hashtable binding=query.getSolution();
		Term value=(Term)binding.get(PlBoardWrapper.VALUE);
		query.close();
		return value.doubleValue();
	}


}
